#!/usr/bin/python
# -- coding:utf8 --

import app.model_phl.util_log as utils
from app.model_phl.utils import select_sql_mysql_v1
import warnings

warnings.filterwarnings('ignore')


def get_is_none(x):
    if x is None or str(x).split('.')[0].replace(' ', '').lower().strip() in ['-999999', '-999976', '-999977', '-999978',
                                                                              '-999998',
                                                                              '-999999.0', '-999976.0', '-999977.0',
                                                                              '-999978.0','-9999', '-9976', '-9977', '-9978',
                                                                              '-9998',
                                                                              '-9999.0', '-9976.0', '-9977.0',
                                                                              '-9978.0',
                                                                              'none', 'nan', 'nat', '',
                                                                              '000000000000000']:
        return True
    else:
        return False


def get_value(x):
    try:
        if get_is_none(x):
            return -999999
        else:
            return round(float(x), 2)
    except:
        return -999976

def get_device_data(ApplyNO):
    """
    从MySQL获取年龄数据
    :param ApplyNO:
    :return:
    """
    order_sub = str(ApplyNO)[-1:]
    device_sql = f"""
    select 
    ApplyNO
    ,sensorCount
    from Sysdevicedates_{order_sub}
    where ApplyNO = '{ApplyNO}'
    """
    device_data = select_sql_mysql_v1(device_sql)
    return device_data

def get_device_freatures(ApplyNO):
    """
    根据手机号后两位和订单号获取数据并计算APP特征集
    :param phone_sub:
    :param ApplyNO:
    :return:
    """
    device_df = get_device_data(ApplyNO)
    return calculateFreeDeviceFeatures(device_df)

def calculateFreeDeviceFeatures(device_df):
    """
    用户基本信息数据的特征集
    :param ApplyNO:
    :return:
    """
    try:
        if device_df.empty:
            return {
                'deviceSensorCount': -999999
            }
        deviceSensorCount = get_value(device_df.loc[0, 'sensorCount'])

        return {
            'deviceSensorCount': deviceSensorCount,
        }
    except Exception as e:
        utils.get_logger().error(e)
        return {
            'deviceSensorCount': -999976,
        }


if __name__ == '__main__':
    # ApplyNO = '169699371854543670'
    ApplyNO = '169736809782272554'
    print(get_device_freatures(ApplyNO))
